package com.es;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

/**
 *
 */
public class TestDatabaseMetaData {
    
    public static void main(String[] args) throws SQLException, IOException {
        //mysqlDatabaseMetaData();
        //oracleDatabaseMetaData();
        mysqlResultSetMetaData();
    }

    private static void mysqlResultSetMetaData() throws SQLException {

        Connection connection = JDBCUtils.getConnection("mysql");
        DatabaseMetaData metaData = connection.getMetaData();
        ResultSet tables = metaData.getTables("db2202",null,null, null);
        while(tables.next()){
            String table = tables.getString("TABLE_NAME");
            System.out.println("----------------"+table+"--------------------");
            String sql = "select * from "+table;
            //
            PreparedStatement ps = connection.prepareStatement(sql);
            //ps.setString(1, table);
            ResultSet tableData = ps.executeQuery();
            //获取结果集的元数据
            ResultSetMetaData resultSetMetaData = tableData.getMetaData();
            while(tableData.next()) {
                //总列数
                int count = resultSetMetaData.getColumnCount();
                for (int i = 1; i <= count; i++) {
                    System.out.print(resultSetMetaData.getColumnName(i)+"=");
                    //紧跟着他的值
                    System.out.println(tableData.getObject(i));
                    System.out.println();
                }
            }
        }
        // insert into xx (x,x,x) values(x,x,x)
    }

    private static void mysqlDatabaseMetaData() throws SQLException, IOException {
        Connection connection = JDBCUtils.getConnection("mysql");
        DatabaseMetaData metaData = connection.getMetaData();
        //数据库的版本信息
        System.out.println("mysql主版本:"+metaData.getDatabaseMajorVersion());
        System.out.println(metaData.getDatabaseMinorVersion());
        System.out.println(metaData.getDatabaseProductVersion());
        System.out.println(metaData.getDatabaseProductName());
        //数据库驱动的版本信息
        System.out.println(metaData.getDriverVersion());
        System.out.println(metaData.getDriverMajorVersion());
        System.out.println(metaData.getDriverMinorVersion());
        System.out.println(metaData.getDriverName());
        //获取catalog

        ResultSet resultSet = metaData.getCatalogs();
        //
        PrintWriter out = new PrintWriter(new FileWriter("d:/mysql.txt"),true);

        while(resultSet.next()){
            String dbName = resultSet.getString(1);
            //获取某数据库中的所有表
            out.println("-----------------------"+dbName+"-------------------------------");
            //包含的信息有 参考api
            ResultSet tables = metaData.getTables(dbName,null,null,null);
            while(tables.next()){
                String tableName = tables.getString("TABLE_NAME");
                out.print(tableName);
                ResultSet coloums = metaData.getColumns(dbName, null, tableName,null);
                out.print("(");
                while(coloums.next()){
                    out.print(coloums.getString("COLUMN_NAME")+" "+coloums.getString("TYPE_NAME")+"-"+coloums.getString("COLUMN_SIZE")+",");
                }
                out.println(")");
            }
        }

    }
    //交给你们
    private static void oracleDatabaseMetaData() throws SQLException {
        Connection connection = JDBCUtils.getConnection("oracle");
        DatabaseMetaData metaData = connection.getMetaData();
        //数据库的版本信息
        System.out.println("mysql主版本:"+metaData.getDatabaseMajorVersion());
        System.out.println(metaData.getDatabaseMinorVersion());
        System.out.println(metaData.getDatabaseProductVersion());
        System.out.println(metaData.getDatabaseProductName());
        //数据库驱动的版本信息
        System.out.println(metaData.getDriverVersion());
        System.out.println(metaData.getDriverMajorVersion());
        System.out.println(metaData.getDriverMinorVersion());
        System.out.println(metaData.getDriverName());
        //获取catalog

        ResultSet resultSet = metaData.getSchemas();
        //
        while(resultSet.next()){
            System.out.println(resultSet.getString(1));
        }



    }
}
// create table xx();
// insert into xx()